package com.leaves.common.util.office;

import cn.javaex.officejj.excel.ExcelUtils;
import cn.javaex.officejj.word.WordUtils;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2023年02月01日  17时48分00秒
 * @Description: word操作工具类
 * <p>
 * 官网地址：https://doc.javaex.cn/officejj
 */
public class WordUtil {

    /**
     * 通过路径读取Word
     *
     * @param filePath 例如：D:\\123.docx
     * @return
     * @throws FileNotFoundException
     */
    public static XWPFDocument readWordToPath(String filePath) throws FileNotFoundException {
        XWPFDocument word = WordUtils.getDocx(filePath);
        return word;
    }

    /**
     * 通过流读取Word
     *
     * @param inputStream
     * @return
     */
    public static XWPFDocument readWordToInputStream(InputStream inputStream) {
        XWPFDocument word = WordUtils.getDocx(inputStream);
        return word;
    }

    /**
     * 读取resources文件夹下的Word
     *
     * @param filePath resources文件夹下的路径，例如：template/word/模板.docx
     * @return
     * @throws FileNotFoundException
     */
    public static XWPFDocument readWordToResources(String filePath) throws FileNotFoundException {
        XWPFDocument word = WordUtils.getDocx(filePath);
        return word;
    }

    /**
     * 替换Word占位符的内容
     *
     * @param word  Word文件对象
     * @param param 允许为空
     * @return
     * @throws Exception
     */
    public static XWPFDocument writeWord(XWPFDocument word, Map<String, Object> param) throws Exception {
        XWPFDocument docx = WordUtils.writeDocx(word, param);
        return docx;
    }

    /**
     * 预览Word（将Word转为html，支持.doc、.docx）
     * <p>
     * 将word（.docx后缀）转为html
     * <p>
     * 会在word文件的同级目录创建一个 word文件名_html 的文件夹，然后在这个文件夹中生成一个与word同名的html文件
     *
     * @param wordPath word文件的全路径，例如：D:\\Temp\\1.docx
     * @return 返回生成的html文件的全路径，例如：D:\\Temp\\1_html\\1.html
     * @throws Exception
     */
    public static String wordToHtml(String wordPath) throws Exception {
        String htmlPath = WordUtils.wordToHtml(wordPath);
        return htmlPath;
    }

    /**
     * 下载Word
     *
     * @param word     word对象，支持 doc 和 docx，例如：XWPFDocument word
     * @param fileName 带后缀的文件名，例如：test.docx
     * @throws IOException
     */
    public static void downloadWord(POIXMLDocument word, String fileName) throws IOException {
        WordUtils.download(word, fileName);
    }

    /**
     * 下载Word
     *
     * @param response
     * @param word     word对象，支持 doc 和 docx，例如：XWPFDocument word
     * @param fileName 带后缀的文件名，例如：test.docx
     * @throws IOException
     */
    public static void downloadWord(HttpServletResponse response, POIXMLDocument word, String fileName) throws IOException {
        WordUtils.download(response, word, fileName);
    }

    public static void main(String[] args) throws Exception {
//        String wordToHtml = wordToHtml("D:\\test\\office_test\\JAVA日期格式化.docx");
//        System.out.println("wordToHtml = " + wordToHtml);

        String htmlPath = WordUtils.wordToHtml("D:\\test\\office_test\\新建 DOCX 文档.docx");
        System.out.println("htmlPath = " + htmlPath);

//        String htmlPath = ExcelUtils.excelToHtml("D:\\\\test\\\\office_test\\\\1626154134307.xls");
//        System.out.println("htmlPath = " + htmlPath);
    }
}
